package org.example.lys.stream;

import org.example.lys.json.JsonUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author liyishan
 * @date 2023/6/21 10:41
 * @apiNote
 */

public class test01 {

    public static void main(String[] args) {
        List<User> userList = build();
        test10002(userList);
    }

    /**
     *   List<User> userList
     * @param userList   List<User> newUserList
     *  取对象列表的若干属性创建新对象    可修改但不会修改原来的stream
     */
    private static void test1(List<User> userList){
        List<User> result  = userList.stream().map(user -> {
            User myUser = new User();

            myUser.setName(user.getName());
            myUser.setPrice(user.getPrice().add(new BigDecimal(1)));
            return myUser;
        }).collect(Collectors.toList());
        System.out.println("旧"+JsonUtils.toJson(userList));
        System.out.println("新"+JsonUtils.toJson(result));

    }

    /**
     *   List<User> userList
     * @param userList   List<User> userList
     *  取对象列表的若干属性创建新对象    可修改会改变原来的stream
     */
    private static void test1001(List<User> userList){
        List<User> result = userList.stream().peek(u ->{
            u.setPrice(u.getPrice().multiply(new BigDecimal("0.1")));
        }).collect(Collectors.toList());
        System.out.println("旧"+JsonUtils.toJson(userList));
        System.out.println("新"+JsonUtils.toJson(result));

    }


    private static void test10002(List<User> userList){
       List<User> user = userList.stream().filter(y -> y.getName().equals("一")).collect(Collectors.toList());
        System.out.println(JsonUtils.toJson(userList));
        System.out.println(JsonUtils.toJson(user));
    }

    private static void test10003(List<User> userList){
        List<User> user = userList.stream().filter(Objects::nonNull).collect(Collectors.toList());
        System.out.println(JsonUtils.toJson(user));
    }


    private static void test10004(List<User> userList){
        List<User> collect = userList.stream().sorted(Comparator.comparing(f -> f.getPrice())).collect(Collectors.toList());
        System.out.println(JsonUtils.toJson(collect));
    }

    private static void test10005(List<User> userList){
        List<User> collect = userList.stream().filter(f -> f.getPrice().compareTo(new BigDecimal(3)) < 0 ).sorted(Comparator.comparing(User::getPrice).reversed()).collect(Collectors.toList());
        System.out.println(JsonUtils.toJson(collect));
    }

    //返回任何一个如果没有就返回空
    private static void test5(List<User> userList){
        User result= userList.stream().findAny().orElse(null);
        System.out.println(JsonUtils.toJson(result));
    }

    private static void test50001(List<User> userList){
        User result= userList.stream().findAny().orElseThrow(() -> new RuntimeException());
        System.out.println(JsonUtils.toJson(result));
    }

    //返回第一个如果没有就返回空
    private static void test6(List<User> userList){
        User result= userList.stream().findFirst().orElse(null);
        System.out.println(JsonUtils.toJson(result));
    }




    private static List<User> build(){
        List<User> userList = new ArrayList<>();
        User user1= new User();
        user1.setId(1);
        user1.setName("一");
        user1.setPrice(new BigDecimal("1.11"));
        userList.add(user1);
        User user2= new User();
        user2.setId(2);
        user2.setName("二");
        user2.setPrice(new BigDecimal("2.22"));
        userList.add(user2);
        User user3= new User();
        user3.setId(3);
        user3.setName("三");
        user3.setPrice(new BigDecimal("3.33"));
        userList.add(user3);
        User user31= new User();
        user31.setId(4);
        user31.setName("三");
        user31.setPrice(new BigDecimal("3.33"));
        userList.add(user31);
        return userList;
    }


}
